using System;
using System.Collections.Generic;
using System.Linq;
using GameFrameworkLibraly;
using GameFrameworkLibraly.BaseObjects;
using GameFrameworkLibraly.Controls;
using GameFrameworkLibraly.GlobalGameObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GameFramework
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : MyGame
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        //private GameObjectsList<BoxObject> _objects;

        //private BoxObject _boxObject;
        //private AnimatedObjectTest _animatedObjectTest;

        private LoadContentClass _loadContentClass;

        private bool _loaded;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this) {IsFullScreen = true};
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);
            //_objects=new GameObjectsList<BoxObject>();
            TouchPanel.EnabledGestures = GestureType.Tap;
            
            
            
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            _loadContentClass = new LoadContentClass(this,
                                                     new ProgressBarControl(this, new Rectangle(250, 400, 300, 20),
                                                                            Content.Load<Texture2D>("Textures/RopeFront"),
                                                                            Content.Load<Texture2D>("Textures/RopeBack")))
                                    {
                                        DestinationRectangle = new Rectangle(0, 0, 800, 480),
                                        BackgroundTexture = Content.Load<Texture2D>("Textures/Logo"),
                                        BackgroundTexture2 = Content.Load<Texture2D>("Textures/MenuBackground"),
                                        SpriteFont = Content.Load<SpriteFont>("Fonts/Font1"),
                                        TextPosition = new Vector2(123,355),
                                        Text = "Loading Progress"
                                    };

            _loadContentClass.LoadInBGThread();
            _loadContentClass.Loaded += LoadContentClassLoaded;
            _loadContentClass.Closed += LoadContentClassClosed;
            //_boxObject = new BoxObject(this, Content.Load<Texture2D>("Box"));
            //_animatedObjectTest = new AnimatedObjectTest(this, Content.Load<Texture2D>("Explosion"),50,6);
            //_objects.GameObjects.Add(_boxObject);
            // TODO: use this.Content to load your game content here
        }

        void LoadContentClassClosed(object sender, EventArgs e)
        {
            _loaded = true;
        }

        void LoadContentClassLoaded(object sender, EventArgs e)
        {
            _loadContentClass.Text = "WTF??";
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            if (!_loaded)
            {
                _loadContentClass.Update(gameTime);
                while(TouchPanel.IsGestureAvailable)
                {
                    GestureSample gestureSample = TouchPanel.ReadGesture();
                    _loadContentClass.ProcessGesture(gestureSample);
                }
            }

            // TODO: Add your update logic here
            //_boxObject.Update(gameTime);
            //_animatedObjectTest.Update(gameTime););
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            //_boxObject.Draw(gameTime,spriteBatch);
            //_animatedObjectTest.Draw(gameTime,spriteBatch);
            if (!_loaded)
                _loadContentClass.Draw(spriteBatch);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        #region Overrides of MyGame

        public override LoadContentClassBase ContentManager
        {
            get { return _loadContentClass; }
        }

        #endregion
    }
}
